﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;

namespace MvcControlToolkit
{
    /// <summary>
    /// HoverMenu is an ASP.NET AJAX extender that can be attached to any Control, 
    /// and will associate that control with a popup panel to display additional 
    /// content
    /// </summary>
    public static class HoverMenuExtensions
    {
        private static readonly HoverMenuPopupPosition DEFAULT_POSITION = HoverMenuPopupPosition.Left;
        private static readonly int DEFAULT_DELAY = 100;

        /// <summary>
        /// Creates a HoverMenu extender with the specified properties 
        /// </summary>
        /// <param name="elementId">The control that the extender is targeting. When the mouse cursor is over this control, the hover menu popup will be displayed</param>
        /// <param name="popupElementId">The ID of the control to display when mouse is over the target control</param>
        public static string HoverMenu(this AjaxHelper helper, string elementId, string popupElementId)
        {
            return helper.HoverMenu(elementId, popupElementId, "");
        }

        /// <summary>
        /// Creates a HoverMenu extender with the specified properties 
        /// </summary>
        /// <param name="elementId">The control that the extender is targeting. When the mouse cursor is over this control, the hover menu popup will be displayed</param>
        /// <param name="popupElementId">The ID of the control to display when mouse is over the target control</param>
        /// <param name="hoverCssClass">The CSS class to apply to the target when the hover menu popup is visible</param>
        public static string HoverMenu(this AjaxHelper helper, string elementId, string popupElementId, string hoverCssClass)
        {
            return helper.HoverMenu(elementId, popupElementId, hoverCssClass, DEFAULT_POSITION);
        }

        /// <summary>
        /// Creates a HoverMenu extender with the specified properties 
        /// </summary>
        /// <param name="elementId">The control that the extender is targeting. When the mouse cursor is over this control, the hover menu popup will be displayed</param>
        /// <param name="popupElementId">The ID of the control to display when mouse is over the target control</param>
        /// <param name="hoverCssClass">The CSS class to apply to the target when the hover menu popup is visible</param>
        /// <param name="popupPosition">Where the popup should be positioned relative to the target control. Can be Left (Default), Right, Top, Bottom, Center</param>        
        public static string HoverMenu(this AjaxHelper helper, string elementId, string popupElementId, string hoverCssClass, HoverMenuPopupPosition popupPosition)
        {
            return helper.HoverMenu(elementId, popupElementId, hoverCssClass, popupPosition, 0, 0);
        }

        /// <summary>
        /// Creates a HoverMenu extender with the specified properties 
        /// </summary>
        /// <param name="elementId">The control that the extender is targeting. When the mouse cursor is over this control, the hover menu popup will be displayed</param>
        /// <param name="popupElementId">The ID of the control to display when mouse is over the target control</param>
        /// <param name="hoverCssClass">The CSS class to apply to the target when the hover menu popup is visible</param>
        /// <param name="popupPosition">Where the popup should be positioned relative to the target control. Can be Left (Default), Right, Top, Bottom, Center</param>
        /// <param name="offsetX">The number of horizontal pixels to offset the Popup from it's default position, as specified by PopupPosition</param>
        /// <param name="offsetY">The number of vertical pixels to offset the Popup from it's default position, as specified by PopupPosition</param>
        public static string HoverMenu(this AjaxHelper helper, string elementId, string popupElementId, string hoverCssClass, HoverMenuPopupPosition popupPosition, int offsetX, int offsetY)
        {
            return helper.HoverMenu(elementId, popupElementId, hoverCssClass, popupPosition, offsetX, offsetY, DEFAULT_DELAY);
        }

        /// <summary>
        /// Creates a HoverMenu extender with the specified properties
        /// </summary>
        /// <param name="elementId">The control that the extender is targeting. When the mouse cursor is over this control, the hover menu popup will be displayed</param>
        /// <param name="popupElementId">The ID of the control to display when mouse is over the target control</param>
        /// <param name="hoverCssClass">The CSS class to apply to the target when the hover menu popup is visible</param>
        /// <param name="popupPosition">Where the popup should be positioned relative to the target control. Can be Left (Default), Right, Top, Bottom, Center</param>
        /// <param name="offsetX">The number of horizontal pixels to offset the Popup from it's default position, as specified by PopupPosition</param>
        /// <param name="offsetY">The number of vertical pixels to offset the Popup from it's default position, as specified by PopupPosition</param>
        /// <param name="popupDelay">The time, in milliseconds, for the popup to remain visible after the mouse moves away from the target control. Default is 100</param>
        public static string HoverMenu(this AjaxHelper helper, string elementId, string popupElementId, string hoverCssClass, HoverMenuPopupPosition popupPosition, int offsetX, int offsetY, int popupDelay)
        {
            RouteValueDictionary props = new RouteValueDictionary();
            props.Add("popupElement", new ElementReference(popupElementId));
            props.AddIfNotDefault("HoverCssClass", hoverCssClass);
            props.AddIfNotEqual("PopupPosition", popupPosition, DEFAULT_POSITION);
            props.AddIfNotDefault("OffsetX", offsetX);
            props.AddIfNotDefault("OffsetY", offsetY);
            props.AddIfNotEqual("PopDelay", popupDelay, DEFAULT_DELAY);

            return helper.HoverMenu(elementId, props);
        }

        /// <summary>
        /// Creates a HoverMenu extender with the specified properties
        /// </summary>
        /// <param name="elementId">The control that the extender is targeting. When the mouse cursor is over this control, the hover menu popup will be displayed</param>
        /// <param name="properties">An object that contains the properties to be set</param>
        public static string HoverMenu(this AjaxHelper helper, string elementId, object properties)
        {
            var sb = new StringBuilder();

            // Add Microsoft Ajax library   
            sb.Append(helper.MicrosoftAjaxInclude());

            // Add toolkit scripts   
            sb.Append(helper.ToolkitInclude(
                    "AjaxControlToolkit.ExtenderBase.BaseScripts.js",
                    "AjaxControlToolkit.Common.Common.js",
                    "AjaxControlToolkit.DynamicPopulate.DynamicPopulateBehavior.js",
                    "AjaxControlToolkit.Animation.Animations.js",
                    "AjaxControlToolkit.Animation.AnimationBehavior.js",
                    "AjaxControlToolkit.PopupExtender.PopupBehavior.js",
                    "AjaxControlToolkit.HoverExtender.HoverBehavior.js",
                    "AjaxControlToolkit.HoverMenu.HoverMenuBehavior.js"
                ));

            // Perform $create               
            sb.AppendLine(helper.Create("AjaxControlToolkit.HoverMenuBehavior", properties, elementId));

            return sb.ToString();
        }
    }

    #region [Enumerations]

    public enum HoverMenuPopupPosition
    {
        Center = 0,
        Top = 1,
        Left = 2,
        Bottom = 3,
        Right = 4
    }

    #endregion
}
